#include "HandInterface.hpp"
/*
 *  HysteresisGestureClassifier.cpp
 *  
 *
 *  Created by Rasmus Kyng on 14/02/2011.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

using namespace hi;
using namespace cv;

HysteresisGestureClassifier::HysteresisGestureClassifier( Ptr< GeometricGestureClassifier > internalClassifier, int activationThreshold, int deactivationThreshold )
: internalClassifier( internalClassifier ), activationThreshold( activationThreshold ), deactivationThreshold( deactivationThreshold ) {
	
	activationCounter = 0;
	deactivationCounter = 0;
	
}

//DEPRECATED!
bool HysteresisGestureClassifier::classify( Mat& handImg ) {
	return internalClassifier->classify( handImg );
}

bool HysteresisGestureClassifier::segment( Mat& handImg ) {
	return internalClassifier->segment( handImg );
}

bool HysteresisGestureClassifier::isHandActive( Mat& handImg ) {
	
	bool internalActive = internalClassifier->isHandActive( handImg );
	
	if ( internalActive ) {
		deactivationCounter = (deactivationCounter == 0) ? 0 : deactivationCounter-1;
		if ( activationCounter < activationThreshold ) {
			activationCounter++;
			return false;
		} else {
			return true;
		}
	} else {
		activationCounter = (activationCounter == 0) ? 0 : activationCounter-1;
		if ( deactivationCounter < deactivationThreshold ) {
			deactivationCounter++;
			return true;
		} else {
			return false;
		}
	}
	
}

void HysteresisGestureClassifier::updateHist( const cv::Mat& image, const cv::Mat& mask ) {
	return internalClassifier->updateHist( image, mask );
}

double HysteresisGestureClassifier::getRelativeConvexityDefect() {
	return internalClassifier->getRelativeConvexityDefect();
}

double HysteresisGestureClassifier::getContourArea() {
	return internalClassifier->getContourArea();
}

double HysteresisGestureClassifier::getConvexHullArea() {
	return internalClassifier->getConvexHullArea();
}

cv::Mat& HysteresisGestureClassifier::getContourImage() {
	return internalClassifier->getContourImage();
}




//SIMPLER version
//if ( internalActive && counter < thres ) {
//	counter = (thres <= counter) ? thres : counter+1;
//	return counter >= thres;
//} else {
//	counter = (counter <= 0) ? 0 : counter-1;
//	return thres <= counter;
//}